package mx.ipn.presentacion.telefonista;

import mx.ipn.presentacion.telefonista.ui.*;

import com.trolltech.qt.gui.*;
import com.trolltech.qt.core.*;

public class BusquedaBitacora extends QDialog {
	Ui_DialogBusquedaBitacora ui = new Ui_DialogBusquedaBitacora();
	QStandardItemModel modelo;
	MyFiltro filtro;

	private class MyFiltro extends QSortFilterProxyModel {

		private MyFiltro(QObject parent) {
			super(parent);
		}
		
		protected boolean filterAcceptsRow(int sourceRow,
				QModelIndex sourceParent) {
			QModelIndex[] index = new QModelIndex[17];

			for( int i=0; i<17; i++ )
				index[i] = sourceModel().index(sourceRow, i, sourceParent);

			QRegExp filter = filterRegExp();
			QAbstractItemModel model = sourceModel();

			for( int i=0; i<17; i++ )
				if( filter.indexIn(model.data(index[i]).toString()) != -1 )
					return true;
			
			return false;
		}
	}

	public BusquedaBitacora() {
		ui.setupUi(this);

		modelo = crearModeloServicios(this);
		filtro = new MyFiltro(this);
		filtro.setSourceModel(modelo);
		filtro.setDynamicSortFilter(true);

		// ui.tableViewOriginal.setModel(modelo);

		ui.treeViewBitacora.setModel(filtro);
		ui.treeViewBitacora.sortByColumn(1, Qt.SortOrder.AscendingOrder);

		ui.comboBoxTipoFiltro.addItem(tr("Expresión Regular"),
				QRegExp.PatternSyntax.RegExp);
		ui.comboBoxTipoFiltro.addItem(tr("Wildcard"),
				QRegExp.PatternSyntax.Wildcard);
		ui.comboBoxTipoFiltro.addItem(tr("Cadena Fija"),
				QRegExp.PatternSyntax.FixedString);

		ui.checkBoxCaseSensitive.setChecked(true);

		ui.lineEditFiltro.textChanged.connect(this, "textFilterChanged()");
		ui.comboBoxTipoFiltro.currentIndexChanged.connect(this,
				"textFilterChanged()");
		ui.checkBoxCaseSensitive.toggled.connect(this, "textFilterChanged()");
	}

	BusquedaBitacora(QWidget parent) {
		ui.setupUi(this);
	}

	private void textFilterChanged() {
		QRegExp.PatternSyntax syntax;
		int index = ui.comboBoxTipoFiltro.currentIndex();
		syntax = (QRegExp.PatternSyntax) ui.comboBoxTipoFiltro.itemData(index);

		Qt.CaseSensitivity caseSensitivity;
		if (ui.checkBoxCaseSensitive.isChecked())
			caseSensitivity = Qt.CaseSensitivity.CaseSensitive;
		else
			caseSensitivity = Qt.CaseSensitivity.CaseInsensitive;

		QRegExp regExp = new QRegExp(ui.lineEditFiltro.text(), caseSensitivity,
				syntax);

		filtro.setFilterRegExp(regExp);
	}

	private QStandardItemModel crearModeloServicios(QObject padre) {
		QStandardItemModel modelo = new QStandardItemModel(0, 17, padre);

		modelo.setHeaderData(0, Qt.Orientation.Horizontal, tr("Número"));
		modelo.setHeaderData(1, Qt.Orientation.Horizontal, tr("Tipo"));
		modelo.setHeaderData(2, Qt.Orientation.Horizontal, tr("Captura"));
		modelo.setHeaderData(3, Qt.Orientation.Horizontal, tr("Asignación"));
		modelo.setHeaderData(4, Qt.Orientation.Horizontal, tr("Cubierto"));
		modelo.setHeaderData(5, Qt.Orientation.Horizontal, tr("Reservación"));
		modelo.setHeaderData(6, Qt.Orientation.Horizontal, tr("Status"));
		modelo.setHeaderData(7, Qt.Orientation.Horizontal, tr("Cliente"));
		modelo.setHeaderData(8, Qt.Orientation.Horizontal, tr("Telefono"));
		modelo.setHeaderData(9, Qt.Orientation.Horizontal, tr("Dirección"));
		modelo.setHeaderData(10, Qt.Orientation.Horizontal, tr("Destino"));
		modelo.setHeaderData(11, Qt.Orientation.Horizontal, tr("Forma de Pago"));
		modelo.setHeaderData(12, Qt.Orientation.Horizontal, tr("Factura"));
		modelo.setHeaderData(13, Qt.Orientation.Horizontal, tr("Observaciones"));
		modelo.setHeaderData(14, Qt.Orientation.Horizontal, tr("Telefonista"));
		modelo.setHeaderData(15, Qt.Orientation.Horizontal, tr("Operadora"));
		modelo.setHeaderData(16, Qt.Orientation.Horizontal, tr("Chofer"));

		return modelo;
	}

	public void limpiaModelo(){
		modelo.removeRows(0, modelo.rowCount());
	}
	
	public void agregaServicio(String index0,
			String index1, String index2, String index3, String index4, String index5, String index6, String index7, String index8, String index9, String index10, String index11, String index12, String index13, String index14, String index15, String index16) {
		modelo.insertRow(0);
		modelo.setData(modelo.index(0, 0), index0);
		modelo.setData(modelo.index(0, 1), index1);
		modelo.setData(modelo.index(0, 2), index2);
		modelo.setData(modelo.index(0, 3), index3);
		modelo.setData(modelo.index(0, 4), index4);
		modelo.setData(modelo.index(0, 5), index5);
		modelo.setData(modelo.index(0, 6), index6);
		modelo.setData(modelo.index(0, 7), index7);
		modelo.setData(modelo.index(0, 8), index8);
		modelo.setData(modelo.index(0, 9), index9);
		modelo.setData(modelo.index(0, 10), index10);
		modelo.setData(modelo.index(0, 11), index11);
		modelo.setData(modelo.index(0, 12), index12);
		modelo.setData(modelo.index(0, 13), index13);
		modelo.setData(modelo.index(0, 14), index14);
		modelo.setData(modelo.index(0, 15), index15);
		modelo.setData(modelo.index(0, 16), index16);

		
		textFilterChanged();
	}

	public static void main(String[] args) {

		QApplication.initialize(args);
		BusquedaBitacora filter = new BusquedaBitacora();
		filter.show();
		QApplication.exec();
	}
}
